//
//  DataBaseHandle.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/27.
//  Copyright © 2016年 light. All rights reserved.
//

#import "DataBaseHandle.h"

@interface DataBaseHandle ()

@property (nonatomic, strong)FMDatabase *myFMDB;

@end


@implementation DataBaseHandle

+ (DataBaseHandle *)shareDataBaseHandle
{
    static DataBaseHandle *dataBase = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataBase = [[DataBaseHandle alloc] init];
        
    });
    return dataBase;
    
}

//static sqlite3 *db = nil;

- (void)openFMDB
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",path);
    
    NSString *sqlitePath = [path stringByAppendingPathComponent:@"dataBase.sqilte"];
    self.myFMDB = [[FMDatabase alloc] initWithPath:sqlitePath];
    BOOL result = [self.myFMDB open];
    if (result) {
        NSLog(@"打开FMDB成功");
        
    }else{
        NSLog(@"打开FMDB失败");
    }
    
    
    
}

////创建故事列表
//- (void)createStoryTable
//{
//    BOOL result = [self.myFMDB executeUpdate:@"create table if not exists everydayStory (id integer primary key autoincrement, itText text, avatar_m text, name text,userName text, view_count text,myName text,date_tour text, myText text,photo_s text)"];
//    if (result) {
//        NSLog(@"创建故事表成功");
//    }else{
//        NSLog(@"创建故事表失败");
//    }
//    
//}



//创建故事列表
- (void)createStoryTable
{
    BOOL result = [self.myFMDB executeUpdate:@"create table if not exists everydayStory (id integer primary key autoincrement, itText text, storyId text)"];
    if (result) {
        NSLog(@"创建故事表成功");
    }else{
        NSLog(@"创建故事表失败");
    }
    
}


- (void)dropStoryTable
{
    NSString *sql = @"drop table everydayStory";
    BOOL result = [self.myFMDB executeUpdate:sql];
    
    if (result) {
        NSLog(@"删除表成功");
    }else{
        
        NSLog(@"删除表失败");
        
    }
    
}




//插入
- (void)insertStoryModel:(SpotModel *)spoMod withId:(NSString *)storyId
{
    NSString *sql = [NSString stringWithFormat:@"insert into everydayStory(itText, storyId) values('%@','%@')",spoMod.itText,storyId];
    
    BOOL result = [self.myFMDB executeUpdate:sql];
    if (result) {
        NSLog(@"插入故事成功");
    }else
    {
        NSLog(@"插入故事失败");
    }
}





- (NSMutableArray *)selectAllStory
{
    NSString *sql = [NSString stringWithFormat:@"select *from everydayStory"];
    FMResultSet *result = [self.myFMDB executeQuery:sql];
    NSMutableArray *arrayAll = [NSMutableArray array];
    if (result) {
        NSLog(@"准备成功");
        while ([result next]) {
            SpotModel *model = [[SpotModel alloc] init];
            model.itText = [result objectForColumnName:@"itText"];
            [arrayAll addObject:model.itText];
        }
        
    }else{
        NSLog(@"准备失败");
    }
    return arrayAll;
}


- (void)deleteEverydayStoryRow:(NSString *)title
{
    SpotModel *model = [[SpotModel alloc] init];
    
    NSString *sql = [NSString stringWithFormat:@"delete from everydayStory where title = '%@'",model.itText];
    BOOL result = [self.myFMDB executeUpdate:sql];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
}











#pragma mark — 写入缓存
-(void)writeCachePlist:(NSString *)namePlist result:(id)result
{
    
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",namePlist]];
  
    NSLog(@"%@",filePath);
    
    //BOOL result = [name writeToFile:txtFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"====%@",filePath);
//    
//    NSDictionary *dic= @{@"asdfs":@"547646"};
//    NSLog(@"====%@",dic);
//
  [result writeToFile:filePath atomically:YES];
    
    

}

#pragma mark — 读取缓存
-(NSDictionary *)readCachePlist:(NSString *)namePlist
{
    
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",namePlist]];
    
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    return resultDic;
}

- (NSArray *)readCaChePlist:(NSString *)namePlist
{
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",namePlist]];
    NSArray *resultArr = [NSArray arrayWithContentsOfFile:filePath];
    return resultArr;
}




@end
